﻿#include "MenuInfo.h"
#include <QMessageBox>
#include <QString>
char *Task_Type[]={"NULL",
	"测控模块类型","调度规约类型",  "面板规约类型","RCS规约类型",
	"LFP规约类型", "无规约类型","直流屏规约类型", "交流屏规约类型",  "GPS规约类型",
	"无规约类型","调度规约类型","NSA规约类型"   
};

MenuInfo::MenuInfo(void)
{
	m_pFieldMenu = NULL;
	m_FieldMenuNum = 0;
//	m_pRelateMenu = NULL;
//	m_RelateMenuNum = 0;

}

MenuInfo::~MenuInfo(void)
{
	ReleaseMenu();
}

//	设置菜单信息

int MenuInfo::SetMenu(int ColumnNum, cColumn_Info *pColumnInfo, int ParentID, int TableId, int App_type)
{
	ReleaseMenu();
	if (!pColumnInfo || ColumnNum<=0)
	{
		return -1;
	}
	
	int	i = 0;
	int	j = 0;
	int	FieldIndex = 0;
	int	RltFieldIndex = 0;
	bool	bNewFlag = false;
	m_FieldMenuNum = 0;	
//	m_RelateMenuNum = 0;
	
	for (i=0; i<ColumnNum; i++)
	{
		if (pColumnInfo[i].clmDisplayNo > 0)
		{
			if (DISPLAY_TYPE_MENU == pColumnInfo[i].clmDisplayType ||
				DISPLAY_TYPE_MULTI_MENU == pColumnInfo[i].clmDisplayType/* ||*/
				/*DISPLAY_TYPE_NODE_CFG_STATE == pColumnInfo[i].clmDisplayType*/) 
			{
				m_FieldMenuNum++;
			}
// 			else if (DISPLAY_TYPE_RELATED == pColumnInfo[i].clmDisplayType )	
// 			{
// 				m_RelateMenuNum++;
// 			}
			}
		}
	if (m_FieldMenuNum<=0)
	{
		return 0;
	}

	m_pFieldMenu = new Field_Menu[m_FieldMenuNum];
//	m_pRelateMenu = new Relate_Menu[m_RelateMenuNum];

	FieldIndex = 0;
	RltFieldIndex = 0;
	for (i=0; i<ColumnNum; i++)
	{
		if (pColumnInfo[i].clmDisplayNo > 0)
		{
			if (DISPLAY_TYPE_MENU == pColumnInfo[i].clmDisplayType ||
				DISPLAY_TYPE_MULTI_MENU == pColumnInfo[i].clmDisplayType /*||*/
				/*DISPLAY_TYPE_NODE_CFG_STATE == pColumnInfo[i].clmDisplayType*/)
			{
				m_pFieldMenu[FieldIndex].ColumnId = pColumnInfo[i].ColumnId;
			
				if(QString::fromLocal8Bit(pColumnInfo[i].ColumnEngName)!="m_ProtType")
				{
					GetMenuInfo(m_pFieldMenu[FieldIndex], pColumnInfo[i].clmMenuName);
				}

				FieldIndex++;
			}
// 			else if (DISPLAY_TYPE_RELATED == pColumnInfo[i].clmDisplayType  &&	
// 				pColumnInfo[i].clmRltAppType > 0 &&
// 				pColumnInfo[i].clmRltTableId > 0)
// 			{
// 				bNewFlag = true;
// 				for (j=0; j<RltFieldIndex; j++)
// 				{
// 					if (m_pRelateMenu[j].RelateAppType == pColumnInfo[i].clmRltAppType &&
// 						m_pRelateMenu[j].RelateTableId == pColumnInfo[i].clmRltTableId &&
// 						0 == memcmp(m_pRelateMenu[j].RelateColName, pColumnInfo[i].clmRltColEngName, COLUMN_ENG_NAME_LEN) )
// 					{
// 						bNewFlag = false;
// 						break;
// 					}
// 				}
// 				m_pRelateMenu[RltFieldIndex].RelateAppType = pColumnInfo[i].clmRltAppType;
// 				m_pRelateMenu[RltFieldIndex].RelateTableId = pColumnInfo[i].clmRltTableId;
// 				memcpy((char*)m_pRelateMenu[RltFieldIndex].RelateColName, (char*)pColumnInfo[i].clmRltColEngName, COLUMN_ENG_NAME_LEN);
// 				memcpy((char*)m_pRelateMenu[RltFieldIndex].RelateRSColName, (char*)pColumnInfo[i].clmRltRSColEngName, COLUMN_ENG_NAME_LEN);
// 				GetRelateMenu(pNSA3000TDB, m_pRelateMenu[RltFieldIndex]);
// 				RltFieldIndex++;
// 			}
		}
	}
	
	return 1;
}

void MenuInfo::ReleaseMenu()
{
	if (m_pFieldMenu)
	{
		for (int i=0; i<m_FieldMenuNum; i++)
		{
			if ( m_pFieldMenu[i].pMenu)
			{
				delete [](ItemMenu*) m_pFieldMenu[i].pMenu;
				 m_pFieldMenu[i].pMenu = NULL;
			}
		}
		delete [](Field_Menu*)m_pFieldMenu;
		m_pFieldMenu = NULL;
	}
	m_FieldMenuNum = 0;

// 	if (m_pRelateMenu)
// 	{
// 		for (int j=0; j<m_RelateMenuNum; j++)
// 		{
// 			if (m_pRelateMenu[j].pMenu)
// 			{
// 				delete [](ItemMenu*) m_pRelateMenu[j].pMenu;
// 				m_pRelateMenu[j].pMenu = NULL;
// 			}
// 		}
// 		delete [](Relate_Menu*)m_pRelateMenu;
// 		m_pRelateMenu = NULL;
// 	}
// 	m_RelateMenuNum = 0;
}

bool MenuInfo::IsMenuSafe()
{
	if (m_FieldMenuNum>0 && m_pFieldMenu)
	{
		return true;
	}else
	{
		return false;
	}
}

bool MenuInfo::IsRelateMenuSafe()
{
// 	if (m_RelateMenuNum>0 && m_pRelateMenu)
// 	{
// 		return true;
// 	}else
// 	{
// 		return false;
// 	}
		return false;
}

//	通过域信息和实际值得到菜单名称和对应序号
int MenuInfo::GetMenuShowText(int columnId, int FieldValue, QString &MenuText)
{
	MenuText.clear();
	if (columnId<=0 )
		return -1;
	if (IsMenuSafe())
	{
		int j=0; 
		for (int i=0; i<m_FieldMenuNum; i++)
		{
			if (m_pFieldMenu[i].ColumnId == columnId &&
				m_pFieldMenu[i].pMenu && 
				m_pFieldMenu[i].menuNum>0)
			{
				for (j=0; j<m_pFieldMenu[i].menuNum; j++)
				{
					if (FieldValue == m_pFieldMenu[i].pMenu[j].ActVal &&
						m_pFieldMenu[i].pMenu[j].pDisplayText)
					{
						MenuText = QString::fromLocal8Bit(m_pFieldMenu[i].pMenu[j].pDisplayText);
						return 1;
					}
				}
			}
		}
	}
	return -1;
}

//	通过菜单名得到菜单显示文字
int MenuInfo::GetMenuShowTextByMenuName(char *MenuName, int FieldValue, QString &MenuText)
{
	MenuText.clear();
	Field_Menu m_FieldMenu_Temp;
	bool isFind=false;
	int ret=GetMenuInfo(m_FieldMenu_Temp, MenuName);
	if (ret>0)
	{
		if (m_FieldMenu_Temp.pMenu && 
			m_FieldMenu_Temp.menuNum>0)
		{
			for (int i=0; i<m_FieldMenu_Temp.menuNum; i++)
			{
				if (FieldValue == m_FieldMenu_Temp.pMenu[i].ActVal &&
					m_FieldMenu_Temp.pMenu[i].pDisplayText)
				{
					MenuText = QString::fromLocal8Bit(m_FieldMenu_Temp.pMenu[i].pDisplayText);
					isFind=true;
					break;
				}
			}
		}
		if (!isFind)
		{
			MenuText = QString::fromLocal8Bit(m_FieldMenu_Temp.pMenu[0].pDisplayText);
		}
		return 1;
	}
	else
	{
		return -2;
	}
}

// 通过域信息和实际值得到关联菜单显示文字
int MenuInfo::GetRelateMenuShowText(int RltAppType, int RltTableId, char *pRltColName, int FieldValue, QString &MenuText)
{
	MenuText.clear();
	if (RltAppType <= 0 || RltTableId<=0 || !pRltColName)
		return -1;
	/*
	if (IsRelateMenuSafe())
	{
		int j=0; 
		for (int i=0; i<m_RelateMenuNum; i++)
		{
			if (m_pRelateMenu[i].RelateAppType == RltAppType &&
				m_pRelateMenu[i].RelateTableId == RltTableId &&
				0 == memcmp(pRltColName, m_pRelateMenu[i].RelateColName, COLUMN_ENG_NAME_LEN) &&
				m_pRelateMenu[i].pMenu && 
				m_pRelateMenu[i].menuNum>0)
			{
				for (j=0; j<m_pRelateMenu[i].menuNum; j++)
				{
					if (FieldValue == m_pRelateMenu[i].pMenu[j].ActVal &&
						m_pRelateMenu[i].pMenu[j].pDisplayText)
					{
						MenuText = QString::fromLocal8Bit(m_pRelateMenu[i].pMenu[j].pDisplayText);
						return 1;
					}
				}
			}
		}
	}
	*/
	return -1;
}

//	获得多选菜单文字信息
int MenuInfo::GetMultiMenuText(int columnId, int FieldValue, QString &MenuText)
{
	MenuText.clear();
	if (columnId<=0 )
		return -1;
	if (IsMenuSafe())
	{
		int j=0; 
		for (int i=0; i<m_FieldMenuNum; i++)
		{
			if (m_pFieldMenu[i].ColumnId == columnId &&
				m_pFieldMenu[i].pMenu && 
				m_pFieldMenu[i].menuNum>0)
			{
				for (j=0; j<m_pFieldMenu[i].menuNum; j++)
				{
					if ((FieldValue & m_pFieldMenu[i].pMenu[j].ActVal)>0 &&
						m_pFieldMenu[i].pMenu[j].pDisplayText)
					{
						if (!MenuText.isEmpty())
						{
							MenuText += QString(";");
						}
						MenuText += QString::fromLocal8Bit(m_pFieldMenu[i].pMenu[j].pDisplayText);
					}else if (m_pFieldMenu[i].pMenu[j].ErrVal>0 &&
						m_pFieldMenu[i].pMenu[j].pErrText)
					{
						if (!MenuText.isEmpty())
						{
							MenuText += QString(";");
						}
						MenuText += QString::fromLocal8Bit(m_pFieldMenu[i].pMenu[j].pErrText);
					}
				}
				return 1;
			}
		}
	}
	return 0;
}

//	获得当前菜单的所有内容
int MenuInfo::GetMenu(int columnId, ItemMenu *&pItemMenu)
{
	if (columnId<=0)
	{
		return -1;
	}
	pItemMenu = NULL;
	int MenuNum = 0;
	if (IsMenuSafe())
	{	
		for (int i=0; i<m_FieldMenuNum; i++)
		{
			if (m_pFieldMenu[i].ColumnId == columnId &&
				m_pFieldMenu[i].pMenu && 
				m_pFieldMenu[i].menuNum>0)
			{
				pItemMenu = m_pFieldMenu[i].pMenu;
				return m_pFieldMenu[i].menuNum;
			}
		}
	}
	return 0;
}

int MenuInfo::GetMenuInfo(Field_Menu &FieldMenuInfo, char *MenuName)
{
	if (!MenuName)
	{
		return -1;
	}

	char sql[SQL_MAX_LEN];
	char *buff=NULL;
	int buff_len=0;
	memset(sql, 0, SQL_MAX_LEN);
	QString MenuNameStr;
	MenuNameStr = QString::fromLocal8Bit(MenuName);
	int  Textbufflen = 0;

// 	if (QString::fromLocal8Bit(MenuName)=="Protocol")
// 	{
// 		sprintf(sql,"select * from cmenu_info where menuName='%s' order by menuNo",MenuName);
// 	}
	sprintf(sql,"select * from cmenu_info where menuName='%s' order by menuNo",MenuName);
	int MenuNum = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_CD,TABLE_CMENU_INFO, sql, buff, &buff_len);
	if( MenuNum >0 && buff && buff_len>0)
	{
		FieldMenuInfo.menuNum = MenuNum;
		FieldMenuInfo.pMenu = new ItemMenu[FieldMenuInfo.menuNum];
		for(int i=0; i<FieldMenuInfo.menuNum; i++)
		{
			FieldMenuInfo.pMenu[i].ActVal = ((cMenu_Info*)buff)[i].menuActValue;
			FieldMenuInfo.pMenu[i].SetDisText(((cMenu_Info*)buff)[i].menuDisplayValue, sizeof(((cMenu_Info*)buff)[i].menuDisplayValue));
			if (((cMenu_Info*)buff)[i].menuActErrValue>0)
			{	
				FieldMenuInfo.pMenu[i].ErrVal = ((cMenu_Info*)buff)[i].menuActErrValue;
				FieldMenuInfo.pMenu[i].SetErrText(((cMenu_Info*)buff)[i].menuDisplayErrValue, sizeof(((cMenu_Info*)buff)[i].menuDisplayErrValue));
			}
		}
	}else
	{
		if (buff)
		{
			delete []buff;
			buff=NULL;
		}
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("数据库中无“%1”菜单").arg(QString::fromLocal8Bit(MenuName)));
		return -1;
	}

	if (buff)
	{
		delete []buff;
		buff=NULL;
	}
	return FieldMenuInfo.menuNum;
}

QString	MenuInfo::GetMenuNameByTaskType(int TaskType)
{
	QString MenuName="";
	if(TaskType>=ENUM_TASK_FOR_MODEL && TaskType<ENUM_TASK_TYPE_NUM)
		MenuName=QString::fromLocal8Bit(Task_Type[TaskType]);
	return MenuName;
}

/*int MenuInfo::GetRelateMenu(Relate_Menu &FieldMenuInfo)
{
	if (FieldMenuInfo.RelateAppType<=0 || FieldMenuInfo.RelateTableId<=0)
	{
		return -1;
	}

	char	sql[SQL_MAX_LEN];
	char	*buff=NULL;
	int	buff_len=0;

	char	*pEngName = NULL;
	int	EngName_len = 0;
	int	Result = 0;
	int	RltColLen = 0;
	int	RltShowColLen = 0;

	Result = g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(FieldMenuInfo.RelateAppType,FieldMenuInfo.RelateTableId, pEngName, &EngName_len);
	if (Result <=0 || !pEngName || EngName_len <= 0)
	{
		if (pEngName)
		{
			delete []pEngName;
			pEngName = NULL;
		}
		return -2;
	}
	
	memset(sql, 0, SQL_MAX_LEN);
	if (strlen(FieldMenuInfo.RelateColName) > 0 && 
		strlen(FieldMenuInfo.RelateRSColName) > 0 )
	{
		sprintf(sql, "select %s, %s from %s ", FieldMenuInfo.RelateColName, FieldMenuInfo.RelateRSColName, pEngName);
	}else if (strlen(FieldMenuInfo.RelateRSColName) > 0 )
	{
		sprintf(sql, "select ID, %s from %s ", FieldMenuInfo.RelateRSColName, pEngName);
	}else if (strlen(FieldMenuInfo.RelateColName) > 0 )
	{
		sprintf(sql, "select %s, Name from %s ", FieldMenuInfo.RelateColName, pEngName);
	}else
	{
		sprintf(sql, "select ID, Name from %s ", pEngName);
	}
	Result = g_DbCfgFunObj.GetColLenbyAppType_TableId_ColEnName(FieldMenuInfo.RelateTableId, FieldMenuInfo.RelateAppType, FieldMenuInfo.RelateColName, RltColLen);
	if (RltColLen<=0)
	{
		RltColLen = sizeof(unsigned int);
	}
	Result = g_DbCfgFunObj.GetColLenbyAppType_TableId_ColEnName(FieldMenuInfo.RelateTableId, FieldMenuInfo.RelateAppType, FieldMenuInfo.RelateRSColName, RltShowColLen);
	if (RltShowColLen <= 0)
	{
		RltShowColLen = STRING_NORMAL_LEN;
	}
	
	Result = g_pSqLiteDB.DB_SelectByTableId(FieldMenuInfo.RelateAppType,FieldMenuInfo.RelateTableId, sql, buff, &buff_len);
	if( Result >0 && buff && buff_len>0)
	{
		FieldMenuInfo.menuNum = Result;
		FieldMenuInfo.pMenu = new ItemMenu[FieldMenuInfo.menuNum];
		int	ActValue = 0;
		int	Index = 0;
		for(int i=0; i<FieldMenuInfo.menuNum; i++)
		{
			if (Index+RltColLen+RltShowColLen <= buff_len)
			{
				memcpy(&ActValue, buff+Index, RltColLen);
				FieldMenuInfo.pMenu[i].ActVal = FieldMenuInfo.RelateTableId*PARENT_OFFSET+ActValue;
				Index += RltColLen;
				FieldMenuInfo.pMenu[i].SetDisText(buff+Index, RltShowColLen);
				Index += RltShowColLen;
			}
		}
	}else
	{
		if (buff)
		{
			delete []buff;
			buff=NULL;
		}
	//	QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("数据库中无关联菜单")));
		return -1;
	}

	if (buff)
	{
		delete []buff;
		buff=NULL;
	}

	return FieldMenuInfo.menuNum;
}*/